package com.sinder.campus_prophet.web.jwgl;

import cc.lyceum.api.thxy.jwgl.ThxyJwgl;
import com.sinder.campus_prophet.core.annotation.RequestLimit;
import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.service.jwgl.TbCultureProgramService;
import com.sinder.campus_prophet.service.jwgl.TbJwglInfoService;
import com.sinder.campus_prophet.service.jwgl.bean.dto.CultureProgramSynDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * 培养方案，绩点数据接口
 *
 * @author sinder
 * Date 2023/3/20/22:50
 **/
@RestController
@RequestMapping("culture")
@Slf4j
public class CultureProgramController {
    private final TbCultureProgramService cultureProgramService;

    private final TbJwglInfoService jwglInfoService;

    public CultureProgramController(TbCultureProgramService cultureProgramService, TbJwglInfoService jwglInfoService) {
        this.cultureProgramService = cultureProgramService;
        this.jwglInfoService = jwglInfoService;
    }

    /**
     * 查询培养方案数据
     * @param openId 用户标识
     * @return 统一响应
     */
    @GetMapping("getCultureProgram")
    public ResponseMessage getCultureProgram(@NotBlank(message = "用户标识不能为空") String openId) {
        return cultureProgramService.getCultureProgram(openId);
    }

    /**
     * 培养方案数据单独同步
     * RequestLimit(count=1, time=1800000)：半小时内不能超过1次同步操作，避免重复点击，恶意请求
     * @param cultureProgramSynDto 用户标识
     * @return 统一响应
     */
    @RequestLimit(count = 3, time = 600000)
    @PostMapping("synCultureProgramData")
    public ResponseMessage synCultureProgramData(@Validated @RequestBody CultureProgramSynDto cultureProgramSynDto, HttpServletRequest request) {
        // 先校验账号密码是否正确
        ThxyJwgl jwgl = jwglInfoService.checkJwglLogin(cultureProgramSynDto.getOpenId(), request);
        return cultureProgramService.synCultureProgramData(cultureProgramSynDto, jwgl);
    }

}
